<?php
class Macys_Api_V2_Shoppingbag_Abstract extends Macys_Api_V2_Abstract {

	CONST SESSION_NAME = "Shoppingbag";
	CONST BCOMCOOKIE = "bloomingdales_online_uid";
	CONST MCOMCOOKIE = "macys_online_uid";

	protected $cookieFlagAtRequest = TRUE;
	protected $cookieFlagAtResponse = TRUE;

	private $session;


	public $cookieInfo = null;
	protected $shoppingbagEndpoint = "";

	/**
	 * Cookie Info
	 */
	protected $userId;
	protected $bagId;
	protected $processWithCookie = true;
	private $cookieName;


	final protected function setup() {
		$this->session = new Macys_Base_Session();
		$this->sessionData('shoppingbag','dummy');
		$this->keyName = "v2shoppingbag";
		$this->endPoint = "/v2/shoppingbag";
		$this->acceptType = self::JSON;



		// Initialize Cookie Name to be Recoginzed
		if($this->site == Macys_Base_Config::BCOMSITE) {
			$this->cookieName = self::BCOMCOOKIE;
		}
		if($this->site == Macys_Base_Config::MCOMSITE) {
			$this->cookieName = self::MCOMCOOKIE;
		}
	}

	protected function processAdditionalInputParams($params) {
		$input = array();
		foreach($this->allowedParams as $value) {
			if(isset($params[$value])) {
				if(!empty($params[$value])) {
					$this->log->debug("Shopping bag Params : " . $value);
					$input[$value] = $params[$value];
				}
				unset($params[$value]);
			}
		}
		if(array_count_values($input) > 0)
		$this->setItemsRequest($input);
	}

	protected function beforeRequest() {
		if($this->cookieFlagAtRequest)
		$this->preProcessCookieInfo();
	}

	private function getUserId() {
		$id = $this->sessionData('online_uid');
		if(!empty($id)) {
			$this->userId = $id;
		}
		else {
			$user = Macys_Api_V2_User2::getInstance();
			$this->userId = $user->getUserId();
		}
		return $this->userId;
	}

	private function setUserId($userId) {
		$this->log->debug("UserId found in Shopping bag Cookie : " . $userId);
		$this->sessionData('online_uid', $userId);
	}




	private function preProcessCookieInfo() {
		$uid = $this->getUserId();
		if(!empty($uid)) {
			// TODO need to do for MERGE BAG
			$cooki = $this->sessionData('cookieData');
			$this->log->debug("User ID retrived from Session: " . $uid);
			$this->log->debug("Cookie From Session Data : " . $cooki);
			//$cookie = Zend_Http_Cookie::fromString($cooki);
			//$this->_client->setCookieJar();
			//$this->_client->setCookie($cookie);
			$this->_client->setCookie($cooki);
			/*$jar = new Zend_Http_CookieJar();
			 $jar->addCookie($cooki);
			$this->_client->setCookieJar($jar);*/
		}
		else {
			$this->log->debug("No Cookie Retrived From Session Data");
			$this->_client->setCookieJar();
		}
	}

	private function postProcessCookieInfo() {
		$x = $this->_client->getCookieJar();
		if(!empty($x)) {
			$this->cookieInfo = $x->getAllCookies(Zend_Http_CookieJar::COOKIE_OBJECT);
			if(!empty($this->cookieInfo)) {
				foreach ($this->cookieInfo as $key => $value) {
					if($value instanceof Zend_Http_Cookie) {
						$this->log->debug("From Config Cookie Name : " . $this->cookieName);
						$this->log->debug("Cookie Identified with name :: " . $value->getName());
						if($value->getName() == $this->cookieName) {
							$uid = $value->getValue();
							$this->log->debug("User Id found in cookie :: " . $this->cookieName . "=" . $uid );
							$this->setUserId($uid);
							$this->sessionData('cookieData', $value);
						}
					}
				}
			}
		}
		else {
			$this->log->debug("No COOKIE DATA Found Hence Not using it");
		}
	}

	public function getCookieInfo() {
		return $this->cookieInfo;
	}

	final protected function afterRequest(){
		if($this->cookieFlagAtResponse)
		$this->postProcessCookieInfo();
	}


	final protected function prepareUri() {
		$this->currentUri = $this->uri . $this->endPoint . $this->shoppingbagEndpoint;
	}

	/**
	 * Saves this Class's info to Session
	 * Provides connectivity to Session class
	 * @param string $key
	 * @param Mixed $value
	 */
	private function sessionData($key, $value = null) {
		return $this->session->sessionData(self::SESSION_NAME, $key, $value);
	}
}